package factory

import (
	"github.com/bwmarrin/snowflake"
	"github.com/redis/go-redis/v9"
	"github.com/spf13/viper"
	"gorm.io/gorm"
	"xincan.com.cn/tarot-go/internal/common/types"
	"xincan.com.cn/tarot-go/internal/components/logging"
)

// ConfigWatchRemoteConfigLoadFunc
// Title    			ConfigWatchRemoteConfigLoadFunc
// Description   		远程配置中心监听参数
// Author      			alittlexincan@163.com       			2024/07/17 11:36
// Update    			alittlexincan@163.com       			2024/07/17 11:36
type ConfigWatchRemoteConfigLoadFunc func(watchViper *viper.Viper)

// BaseConfigLoaderFactory
// Title    			BaseConfigLoaderFactory
// Description   		基础配置加载器接口
// Author      			alittlexincan@163.com       			2024/07/17 11:36
// Update    			alittlexincan@163.com       			2024/07/17 11:36
type BaseConfigLoaderFactory[V any, T any] interface {

	// SetSelf 设置配置加载当前本身实例
	SetSelf(self T) T

	// WithApplicationConfig 整体系统配置
	WithApplicationConfig(config *types.Service) T

	// WithLogger 日志实例配置
	WithLogger(trace *logging.TraceLogger) T

	// ConfigLoad 加载器接口
	ConfigLoad() (V, T)

	// GetLogger 获取日志实例
	GetLogger() *logging.TraceLogger
}

// BaseConfigLoader
// Title    			BaseConfigLoader
// Description   		基础配置加载器结构体
// Author      			alittlexincan@163.com       			2024/07/17 11:36
// Update    			alittlexincan@163.com       			2024/07/17 11:36
type BaseConfigLoader[T any] struct {

	// Self 配置加载器实例
	Self T

	// App 配置实例
	App *types.Service

	// Logger 日志实例
	Logger *logging.TraceLogger
}

// SetSelf
// Title    			SetSelf
// Description   		设置配置加载器实例
// Author      			alittlexincan@163.com       						2024/07/17 11:36
// Update    			alittlexincan@163.com       						2024/07/17 11:36
// Param    			self 						T						配置加载器实例
// Return    			T     												返回配置加载器实例
func (base *BaseConfigLoader[T]) SetSelf(self T) T {
	base.Self = self
	return base.Self
}

// WithApplicationConfig
// Title    			WithApplicationConfig
// Description   		设置系统配置实例
// Author      			alittlexincan@163.com       						2024/07/17 11:36
// Update    			alittlexincan@163.com       						2024/07/17 11:36
// Param    			config 						*types.Service			系统配置实例
// Return    			T     												返回配置加载器实例
func (base *BaseConfigLoader[T]) WithApplicationConfig(config *types.Service) T {
	base.App = config
	return base.Self
}

// WithLogger
// Title    			WithLogger
// Description   		设置日志实例
// Author      			alittlexincan@163.com       						2024/07/17 11:36
// Update    			alittlexincan@163.com       						2024/07/17 11:36
// Param    			logger 						*logging.TraceLogger	日志实例
// Return    			T     												返回配置加载器实例
func (base *BaseConfigLoader[T]) WithLogger(logger *logging.TraceLogger) T {
	base.Logger = logger
	return base.Self
}

// GetLogger
// Title    			GetLogger
// Description   		获取日志实例
// Author      			alittlexincan@163.com       						2024/07/17 11:36
// Update    			alittlexincan@163.com       						2024/07/17 11:36
// Return    			*logging.TraceLogger     							返回日志实例
func (base *BaseConfigLoader[T]) GetLogger() *logging.TraceLogger {
	return base.Logger
}

// RemoteApplicationConfigLoaderFactory
// Title    			RemoteApplicationConfigLoaderFactory
// Description   		远程配置中心加载器接口
// Author      			alittlexincan@163.com       			2024/07/17 11:36
// Update    			alittlexincan@163.com       			2024/07/17 11:36
type RemoteApplicationConfigLoaderFactory interface {

	// BaseConfigLoaderFactory 基础配置加载器接口
	BaseConfigLoaderFactory[*viper.Viper, RemoteApplicationConfigLoaderFactory]

	// WatchRemoteConfigLoad 监听远程配置中心配置
	WatchRemoteConfigLoad(callback ConfigWatchRemoteConfigLoadFunc)
}

// ApplicationRegistryLoaderFactory
// Title    			ApplicationRegistryLoaderFactory
// Description   		注册中心加载器接口
// Author      			alittlexincan@163.com       			2024/07/17 11:36
// Update    			alittlexincan@163.com       			2024/07/17 11:36
type ApplicationRegistryLoaderFactory interface {

	// BaseConfigLoaderFactory 基础配置加载器接口
	BaseConfigLoaderFactory[interface{}, ApplicationRegistryLoaderFactory]
}

// ApplicationCacheLoaderFactory
// Title    			ApplicationCacheLoaderFactory
// Description   		缓存信息加载器接口
// Author      			alittlexincan@163.com       			2024/07/17 11:36
// Update    			alittlexincan@163.com       			2024/07/17 11:36
type ApplicationCacheLoaderFactory interface {

	// BaseConfigLoaderFactory 基础配置加载器接口
	BaseConfigLoaderFactory[*redis.Client, ApplicationCacheLoaderFactory]
}

// ApplicationOrmLoaderFactory
// Title    			ApplicationOrmLoaderFactory
// Description   		对象关系映射信息加载器接口
// Author      			alittlexincan@163.com       			2024/07/17 11:36
// Update    			alittlexincan@163.com       			2024/07/17 11:36
type ApplicationOrmLoaderFactory interface {

	// BaseConfigLoaderFactory 基础配置加载器接口
	BaseConfigLoaderFactory[*gorm.Config, ApplicationOrmLoaderFactory]
}

// ApplicationDatabaseLoaderFactory
// Title    			ApplicationDatabaseLoaderFactory
// Description   		数据库信息加载器接口
// Author      			alittlexincan@163.com       			2024/07/17 11:36
// Update    			alittlexincan@163.com       			2024/07/17 11:36
type ApplicationDatabaseLoaderFactory interface {

	// BaseConfigLoaderFactory 基础配置加载器接口
	BaseConfigLoaderFactory[*gorm.DB, ApplicationDatabaseLoaderFactory]

	// WithOrmConfig 宽度对象关系映射配置
	WithOrmConfig(ormConfig *gorm.Config) ApplicationDatabaseLoaderFactory

	// WithSnowflake 雪花算法配置
	WithSnowflake(snowflake *snowflake.Node) ApplicationDatabaseLoaderFactory

	// WithMigrateModel 添加迁移模型
	WithMigrateModel(model interface{}) ApplicationDatabaseLoaderFactory
}
